package sadmin

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"shop/models/sadmin"
)

type TypeController struct {
	beego.Controller
}
//@Title typeadd
//@Description 类型管理
//@Param body body sadmin.Typ true "d"
//@Success 200 ok
//@router /type/add/ [post]
func (c *TypeController)TypeAdd()  {
	var com sadmin.Typ
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
	err:=sadmin.TypeAdd(com)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]="ok"
	}
	c.ServeJSON()
}
//@Title typeedit
//@Description 类型编辑
//@Param body body sadmin.Typ true "d"
//@Success 200 ok
//@router /type/edit [put]
func (c *TypeController)TypeEdit()  {

	var com sadmin.Typ
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
	err:=sadmin.TypeEdit(com)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}
	c.ServeJSON()
}

//@Title typeshow
//@Description 展示类型信息
//@Success 200 {object} sadmin.Typ
//@router /typeshow [get]
func (c *TypeController)TypeShow()  {
	var com []sadmin.Typ
	com, err := sadmin.TypeShow()
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"] = com
	}

	c.ServeJSON()
}
//@Title typeshowbyid
//@Description 展示单个类型信息
//@Param id query int true "id"
//@Success 200 {object} sadmin.Typ
//@router /type/showbyid/ [get]
func (c *TypeController)TypeShowById(id int)  {
	com:=sadmin.TypeShowById(id)
	c.Ctx.ResponseWriter.WriteHeader(200)
	c.Data["json"]=com
	c.ServeJSON()
}
// @Title typedel
// @Description 删除类型
// @Param	id		path 	int	true		"The id of type"
// @Success 200 ok
// @router /type/del/:id [delete]
func (c *TypeController)TypeDel()  {

	idd,_:=c.GetInt(":id")
	err:=sadmin.TypeDel(idd)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]="ok"
	}
	c.ServeJSON()
}